package cruds.accessories;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

import services.StockableService;
import utils.StaticMethods;
import items.Stockable;
import home.Home;
import cruds.CrudModifyParent;
import dynamicmenus.AccessoriesMenu;
import exceptions.EmptyFieldException;

@SuppressWarnings("serial")
public class CrudModifyAccessory extends CrudModifyParent<Stockable>{

	private String brand;
	private String model;
	private String measure;
	
	public CrudModifyAccessory(Home home, Stockable item) {
		super(home, item);
	}

	@Override
	protected void redirectIfCancel(AjaxRequestTarget target) {
		final StockableService service = ((StockableService)this.getService());
		AccessoryList panel = new AccessoryList(this.getHome(),service.searchAccessories(this.getItem().getName()));
		panel.changePanel(target);
	}

	@Override
	protected void doUpdateAndRedirect(AjaxRequestTarget target) {
		try{
			StaticMethods.accesoryHasEmptyFields(this.getItem());
			final StockableService service = ((StockableService)this.getService());
			this.getItem().setBrand(brand);
			this.getItem().setModel(model);
			this.getItem().setMeasure(measure);
			service.update(this.getItem());
			AccessoryList panel = new AccessoryList(this.getHome(),service.searchAccessories(this.getItem().getName()));
			panel.changePanel(target);		
		}catch(EmptyFieldException e){
			this.setErrorMessage(target, e.getMessage());
		}
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
		AccessoriesMenu menu = new AccessoriesMenu(this.getHome());
		menu.changePanel(target);
	}

	@Override
	protected void createFormFields(Form<Stockable> form) {
		
		this.setBrand(this.getItem().getBrand());
		this.setModel(this.getItem().getModel());
		this.setMeasure(this.getItem().getMeasure());
		
		final StockableService service = ((StockableService)this.getService());
		final List<String> marcas = service.getBrandService().getCarBrands();
		final WebMarkupContainer cont = new WebMarkupContainer("cont");
		cont.setOutputMarkupId(true);
		form.add(cont);
		
		final DropDownChoice<String> marks = new DropDownChoice<String>("brands", new PropertyModel<String>(this,"brand"), new ArrayList<String>(marcas));
		final DropDownChoice<String> models = new DropDownChoice<String>("models", new PropertyModel<String>(this,"model"), new LoadableDetachableModel<List<String>>(){

			@Override
			protected List<String> load() {
				final String mark = marks.getModelObject();
				final List<String> list = service.getBrandService().getModelsOfMark(mark);
	            return list != null ? list : new ArrayList<String>(0);
			}
		}); 
		
		marks.add(new AjaxFormComponentUpdatingBehavior("onchange"){

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(cont);
			}	
		});
		
		cont.add(marks);
		cont.add(models); 
		
		DropDownChoice<String> measures = new DropDownChoice<String>("measure", new PropertyModel<String>(this, "measure")
				, Arrays.asList("- - -","13''", "14''", "5''", "15''"));
		form.add(measures);
		
		form.add(new TextField<String>("listPrice"));
		form.add(new TextField<Double>("purchasePercentage"));
		form.add(new TextField<Double>("salePercentage"));
		form.add(new TextField<Integer>("stock"));
	}

	@Override
	protected void initializeService() {
		this.setService(this.getHome().getGeneralService().getStockableSer());
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getMeasure() {
		return measure;
	}

	public void setMeasure(String measure) {
		this.measure = measure;
	}



}
